﻿using System;
using System.Collections.Generic;
using System.Text;
using HslCommunication.BasicFramework;

namespace HslCommunication
{
	/// <summary>
	/// 用于本程序集访问通信的暗号说明
	/// </summary>
	// Token: 0x0200000F RID: 15
	internal class HslProtocol
	{
		/// <summary>
		/// 生成终极传送指令的方法，所有的数据均通过该方法出来
		/// </summary>
		/// <param name="command">命令头</param>
		/// <param name="customer">自用自定义</param>
		/// <param name="token">令牌</param>
		/// <param name="data">字节数据</param>
		/// <returns>包装后的数据信息</returns>
		// Token: 0x06000089 RID: 137 RVA: 0x00004184 File Offset: 0x00002384
		internal static byte[] CommandBytes(int command, int customer, Guid token, byte[] data)
		{
			int value = 3001;
			int num = (data == null) ? 0 : data.Length;
			byte[] array = new byte[32 + num];
			BitConverter.GetBytes(command).CopyTo(array, 0);
			BitConverter.GetBytes(customer).CopyTo(array, 4);
			BitConverter.GetBytes(value).CopyTo(array, 8);
			token.ToByteArray().CopyTo(array, 12);
			bool flag = num > 0;
			if (flag)
			{
				BitConverter.GetBytes(num).CopyTo(array, 28);
				Array.Copy(data, 0, array, 32, num);
				HslSecurity.ByteEncrypt(array, 32, num);
			}
			return array;
		}

		/// <summary>
		/// 解析接收到数据，先解压缩后进行解密
		/// </summary>
		/// <param name="head">指令头</param>
		/// <param name="content">指令的内容</param>
		/// <return>真实的数据内容</return>
		// Token: 0x0600008A RID: 138 RVA: 0x0000421C File Offset: 0x0000241C
		internal static byte[] CommandAnalysis(byte[] head, byte[] content)
		{
			bool flag = content != null;
			byte[] result;
			if (flag)
			{
				int num = BitConverter.ToInt32(head, 8);
				bool flag2 = num == 3002;
				if (flag2)
				{
					content = SoftZipped.Decompress(content);
				}
				result = HslSecurity.ByteDecrypt(content);
			}
			else
			{
				result = null;
			}
			return result;
		}

		/// <summary>
		/// 获取发送字节数据的实际数据，带指令头
		/// </summary>
		/// <param name="customer">用户数据</param>
		/// <param name="token">令牌</param>
		/// <param name="data">字节信息</param>
		/// <returns>包装后的指令信息</returns>
		// Token: 0x0600008B RID: 139 RVA: 0x00004260 File Offset: 0x00002460
		internal static byte[] CommandBytes(int customer, Guid token, byte[] data)
		{
			return HslProtocol.CommandBytes(1002, customer, token, data);
		}

		/// <summary>
		/// 获取发送字节数据的实际数据，带指令头
		/// </summary>
		/// <param name="customer">用户数据</param>
		/// <param name="token">令牌</param>
		/// <param name="data">字符串数据信息</param>
		/// <returns>包装后的指令信息</returns>
		// Token: 0x0600008C RID: 140 RVA: 0x00004280 File Offset: 0x00002480
		internal static byte[] CommandBytes(int customer, Guid token, string data)
		{
			bool flag = data == null;
			byte[] result;
			if (flag)
			{
				result = HslProtocol.CommandBytes(1001, customer, token, null);
			}
			else
			{
				result = HslProtocol.CommandBytes(1001, customer, token, Encoding.Unicode.GetBytes(data));
			}
			return result;
		}

		/// <summary>
		/// 获取发送字节数据的实际数据，带指令头
		/// </summary>
		/// <param name="customer">用户数据</param>
		/// <param name="token">令牌</param>
		/// <param name="data">字符串数据信息</param>
		/// <returns>包装后的指令信息</returns>
		// Token: 0x0600008D RID: 141 RVA: 0x000042C4 File Offset: 0x000024C4
		internal static byte[] CommandBytes(int customer, Guid token, string[] data)
		{
			return HslProtocol.CommandBytes(1005, customer, token, HslProtocol.PackStringArrayToByte(data));
		}

		/// <inheritdoc cref="M:HslCommunication.HslProtocol.PackStringArrayToByte(System.String[])" />
		// Token: 0x0600008E RID: 142 RVA: 0x000042E8 File Offset: 0x000024E8
		internal static byte[] PackStringArrayToByte(string data)
		{
			return HslProtocol.PackStringArrayToByte(new string[]
			{
				data
			});
		}

		/// <summary>
		/// 将字符串打包成字节数组内容
		/// </summary>
		/// <param name="data">字符串数组</param>
		/// <returns>打包后的原始数据内容</returns>
		// Token: 0x0600008F RID: 143 RVA: 0x000042FC File Offset: 0x000024FC
		internal static byte[] PackStringArrayToByte(string[] data)
		{
			bool flag = data == null;
			if (flag)
			{
				data = new string[0];
			}
			List<byte> list = new List<byte>();
			list.AddRange(BitConverter.GetBytes(data.Length));
			for (int i = 0; i < data.Length; i++)
			{
				bool flag2 = !string.IsNullOrEmpty(data[i]);
				if (flag2)
				{
					byte[] bytes = Encoding.Unicode.GetBytes(data[i]);
					list.AddRange(BitConverter.GetBytes(bytes.Length));
					list.AddRange(bytes);
				}
				else
				{
					list.AddRange(BitConverter.GetBytes(0));
				}
			}
			return list.ToArray();
		}

		/// <summary>
		/// 将字节数组还原成真实的字符串数组
		/// </summary>
		/// <param name="content">原始字节数组</param>
		/// <returns>解析后的字符串内容</returns>
		// Token: 0x06000090 RID: 144 RVA: 0x0000439C File Offset: 0x0000259C
		internal static string[] UnPackStringArrayFromByte(byte[] content)
		{
			bool flag = content != null && content.Length < 4;
			string[] result;
			if (flag)
			{
				result = null;
			}
			else
			{
				int num = 0;
				int num2 = BitConverter.ToInt32(content, num);
				string[] array = new string[num2];
				num += 4;
				for (int i = 0; i < num2; i++)
				{
					int num3 = BitConverter.ToInt32(content, num);
					num += 4;
					bool flag2 = num3 > 0;
					if (flag2)
					{
						array[i] = Encoding.Unicode.GetString(content, num, num3);
					}
					else
					{
						array[i] = string.Empty;
					}
					num += num3;
				}
				result = array;
			}
			return result;
		}

		/// <summary>
		/// 从接收的数据内容提取出用户的暗号和数据内容
		/// </summary>
		/// <param name="content">数据内容</param>
		/// <returns>包含结果对象的信息</returns>
		// Token: 0x06000091 RID: 145 RVA: 0x00004430 File Offset: 0x00002630
		public static OperateResult<NetHandle, byte[]> ExtractHslData(byte[] content)
		{
			bool flag = content.Length == 0;
			OperateResult<NetHandle, byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateSuccessResult<NetHandle, byte[]>(0, new byte[0]);
			}
			else
			{
				byte[] array = new byte[32];
				byte[] array2 = new byte[content.Length - 32];
				Array.Copy(content, 0, array, 0, 32);
				bool flag2 = array2.Length != 0;
				if (flag2)
				{
					Array.Copy(content, 32, array2, 0, content.Length - 32);
				}
				bool flag3 = BitConverter.ToInt32(array, 0) == 2010;
				if (flag3)
				{
					result = new OperateResult<NetHandle, byte[]>(Encoding.Unicode.GetString(array2));
				}
				else
				{
					int num = BitConverter.ToInt32(array, 0);
					int value = BitConverter.ToInt32(array, 4);
					array2 = HslProtocol.CommandAnalysis(array, array2);
					bool flag4 = num == 6;
					if (flag4)
					{
						result = new OperateResult<NetHandle, byte[]>(Encoding.Unicode.GetString(array2));
					}
					else
					{
						result = OperateResult.CreateSuccessResult<NetHandle, byte[]>(value, array2);
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 规定所有的网络传输指令头都为32字节
		/// </summary>
		// Token: 0x0400004B RID: 75
		internal const int HeadByteLength = 32;

		/// <summary>
		/// 所有网络通信中的缓冲池数据信息
		/// </summary>
		// Token: 0x0400004C RID: 76
		internal const int ProtocolBufferSize = 1024;

		/// <summary>
		/// 用于心跳程序的暗号信息
		/// </summary>
		// Token: 0x0400004D RID: 77
		internal const int ProtocolCheckSecends = 1;

		/// <summary>
		/// 客户端退出消息
		/// </summary>
		// Token: 0x0400004E RID: 78
		internal const int ProtocolClientQuit = 2;

		/// <summary>
		/// 因为客户端达到上限而拒绝登录
		/// </summary>
		// Token: 0x0400004F RID: 79
		internal const int ProtocolClientRefuseLogin = 3;

		/// <summary>
		/// 允许客户端登录到服务器
		/// </summary>
		// Token: 0x04000050 RID: 80
		internal const int ProtocolClientAllowLogin = 4;

		/// <summary>
		/// 客户端登录的暗号信息
		/// </summary>
		// Token: 0x04000051 RID: 81
		internal const int ProtocolAccountLogin = 5;

		/// <summary>
		/// 客户端拒绝登录的暗号信息
		/// </summary>
		// Token: 0x04000052 RID: 82
		internal const int ProtocolAccountRejectLogin = 6;

		/// <summary>
		/// 说明发送的只是文本信息
		/// </summary>
		// Token: 0x04000053 RID: 83
		internal const int ProtocolUserString = 1001;

		/// <summary>
		/// 发送的数据就是普通的字节数组
		/// </summary>
		// Token: 0x04000054 RID: 84
		internal const int ProtocolUserBytes = 1002;

		/// <summary>
		/// 发送的数据就是普通的图片数据
		/// </summary>
		// Token: 0x04000055 RID: 85
		internal const int ProtocolUserBitmap = 1003;

		/// <summary>
		/// 发送的数据是一条异常的数据，字符串为异常消息
		/// </summary>
		// Token: 0x04000056 RID: 86
		internal const int ProtocolUserException = 1004;

		/// <summary>
		/// 说明发送的数据是字符串的数组
		/// </summary>
		// Token: 0x04000057 RID: 87
		internal const int ProtocolUserStringArray = 1005;

		/// <summary>
		/// 请求文件下载的暗号
		/// </summary>
		// Token: 0x04000058 RID: 88
		internal const int ProtocolFileDownload = 2001;

		/// <summary>
		/// 请求文件上传的暗号
		/// </summary>
		// Token: 0x04000059 RID: 89
		internal const int ProtocolFileUpload = 2002;

		/// <summary>
		/// 请求删除文件的暗号
		/// </summary>
		// Token: 0x0400005A RID: 90
		internal const int ProtocolFileDelete = 2003;

		/// <summary>
		/// 文件校验成功
		/// </summary>
		// Token: 0x0400005B RID: 91
		internal const int ProtocolFileCheckRight = 2004;

		/// <summary>
		/// 文件校验失败
		/// </summary>
		// Token: 0x0400005C RID: 92
		internal const int ProtocolFileCheckError = 2005;

		/// <summary>
		/// 文件保存失败
		/// </summary>
		// Token: 0x0400005D RID: 93
		internal const int ProtocolFileSaveError = 2006;

		/// <summary>
		/// 请求文件列表的暗号
		/// </summary>
		// Token: 0x0400005E RID: 94
		internal const int ProtocolFileDirectoryFiles = 2007;

		/// <summary>
		/// 请求子文件的列表暗号
		/// </summary>
		// Token: 0x0400005F RID: 95
		internal const int ProtocolFileDirectories = 2008;

		/// <summary>
		/// 进度返回暗号
		/// </summary>
		// Token: 0x04000060 RID: 96
		internal const int ProtocolProgressReport = 2009;

		/// <summary>
		/// 返回的错误信息
		/// </summary>
		// Token: 0x04000061 RID: 97
		internal const int ProtocolErrorMsg = 2010;

		/// <summary>
		/// 请求删除多个文件的暗号
		/// </summary>
		// Token: 0x04000062 RID: 98
		internal const int ProtocolFilesDelete = 2011;

		/// <summary>
		/// 请求删除文件夹的暗号
		/// </summary>
		// Token: 0x04000063 RID: 99
		internal const int ProtocolFolderDelete = 2012;

		/// <summary>
		/// 请求当前的文件是否存在
		/// </summary>
		// Token: 0x04000064 RID: 100
		internal const int ProtocolFileExists = 2013;

		/// <summary>
		/// 不压缩数据字节
		/// </summary>
		// Token: 0x04000065 RID: 101
		internal const int ProtocolNoZipped = 3001;

		/// <summary>
		/// 压缩数据字节
		/// </summary>
		// Token: 0x04000066 RID: 102
		internal const int ProtocolZipped = 3002;
	}
}
